package com.example.jdautoclick;

import com.example.jdautoclick.comm.AutoRobot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

import java.awt.*;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import com.sun.jna.platform.win32.WinDef;
import org.springframework.beans.factory.annotation.Autowired;

@SpringBootApplication
public class JdAutoClickApplication {

    public static void main(String[] args) throws Exception {
        //启动程序
        //无障碍模式启动1
        SpringApplicationBuilder builder = new SpringApplicationBuilder(JdAutoClickApplication.class);
        builder.headless(false).run(args);

        try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}

        Robot robot = new Robot();

        int boxWidth=210;
        int boxHeight=330;
        int waitTime=2; //8秒，等待抓取8秒
        //初始坐标
        int curPointX=120;
        int curPointY=275;
        int loopNum=50; //循环次数
        int index=1;
        int row=0;
        int total=0; //记录总行数
        int offset=0; //偏移量

        try {
            WinDef.HWND handle = AutoRobot.getWindowHandle("Chrome_WidgetWin_0", "京东购物");
            System.out.println("handle:===>"+handle);
            if (handle != null) {
                //移动窗体
                AutoRobot.moveWindowHandle(handle, 0, 0, 415, 780, false);

                while (true) {
                            //检查是否浏览到底了
                            if(index==4){
                                Point pointQ=AutoRobot.FindTargetImagePositon(robot,"done.png");
                                try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
                                System.out.println(pointQ);
                                if(pointQ.x!=-1 && pointQ.y!=-1){
                                    System.out.println("【到底了】...................停止循环");
                                    break;
                                }
                            }

                            /*System.out.println("退出程序");
                            System.exit(0);*/


                            //检查点击是否返回到了首页，则停止当前脚本
                            //是否一开始就显示分类，如果是则先隐藏一次
                            Point point=AutoRobot.FindTargetImagePositon(robot,"guanzhu.png");
                            try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
                            System.out.println(point);
                            if(point.x!=-1 && point.y!=-1){
                                System.out.println("返回到了首页guanzhu.png...................停止循环");
                                break;
                            }
                            point=AutoRobot.FindTargetImagePositon(robot,"jdiy.png"); //京东自营
                            try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
                            System.out.println(point);
                            if(point.x!=-1 && point.y!=-1){
                                System.out.println("返回到了商品分类jdiy.png...................停止循环");
                                break;
                            }
                            /*point=AutoRobot.FindTargetImagePositon(robot,"ygu.png"); //已关注字眼
                            try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
                            System.out.println(point);
                            if(point.x!=-1 && point.y!=-1){
                                System.out.println("返回到了商品分类ygu.png...................停止循环");
                                break;
                            }*/
                            point=AutoRobot.FindTargetImagePositon(robot,"top.png");
                            try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
                            System.out.println(point);
                            if(point.x!=-1 && point.y!=-1){
                                System.out.println("返回到了首页top.png...................停止循环");
                                break;
                            }
                            point=AutoRobot.FindTargetImagePositon(robot,"search.png");
                            try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
                            System.out.println(point);
                            if(point.x!=-1 && point.y!=-1){
                                System.out.println("返回到了首页search.png...................停止循环");
                                break;
                            }
                           /* point=AutoRobot.FindTargetImagePositon(robot,"homeword.png");
                            try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
                            System.out.println(point);
                            if(point.x!=-1 && point.y!=-1){
                                System.out.println("返回到了首页homeword.png...................停止循环");
                                break;
                            }*/



                            /*System.out.println("退出程序");
                            System.exit(0);*/


                            //点击商品分类
                            /*if(index%2==0){ //偶数
                                robot.mouseMove(curPointX+boxWidth, curPointY+offset+boxHeight*row);
                                row++;
                                total++;
                            }else{ //奇数
                                robot.mouseMove(curPointX, curPointY+offset+boxHeight*row);
                            }*/
                            if(index==1){ //左上角
                                robot.mouseMove(80, 330);
                            }
                            if(index==2){ //右上角
                                robot.mouseMove(280, 330);
                            }
                            if(index==3){ //左下角
                                robot.mouseMove(100, 650);
                            }
                            if(index==4){ //右下角
                                robot.mouseMove(280, 650);
                            }
                            robot.delay(200);
                            AutoRobot.MouseClick(robot);

                            try {TimeUnit.SECONDS.sleep(waitTime);} catch (InterruptedException e) {e.printStackTrace();}


                            //是否出现验证码
                            Point pointV=AutoRobot.FindTargetImagePositon(robot,"valid.png");
                            try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
                            System.out.println(pointV);
                            if(pointV.x!=-1 && pointV.y!=-1){
                                System.out.println("###出现验证码###...................停止循环");
                                break;
                            }


                            //移动左上角返回
                            //System.out.println("移动左上角返回......");
                            robot.mouseMove(23, 25);
                            robot.delay(300);
                            AutoRobot.MouseClick(robot);
                            robot.delay(100);



                            if(index%4==0){
                                System.out.println("index%4==0...........");
                                //检查是否浏览到底了
                                Point pointT=AutoRobot.FindTargetImagePositon(robot,"done_other.png");
                                try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
                                System.out.println(pointT);
                                if(pointT.x!=-1 && pointT.y!=-1){
                                    System.out.println("【到底了done_other】...................停止循环");
                                    break;
                                }else{
                                    System.out.println("【没有找到底部111..】");
                                }
                                pointT=AutoRobot.FindTargetImagePositon(robot,"done.png");
                                try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
                                System.out.println(pointT);
                                if(pointT.x!=-1 && pointT.y!=-1){
                                    System.out.println("【到底了done_other】...................停止循环");
                                    break;
                                }else{
                                    System.out.println("【没有找到底部222..】");
                                }


                                index=1;
                                row=0;
                                robot.mouseMove(curPointX, curPointY+offset);
                                robot.delay(1000);
                                for (int i = 0; i <7; i++) {
                                    robot.mouseWheel(1);
                                    try {TimeUnit.MILLISECONDS.sleep(200);} catch (InterruptedException e) {e.printStackTrace();}
                                }
                                /*offset=60+total;
                                System.out.println("total:"+total+",偏移量:"+offset);*/
                                continue;
                            }

                            loopNum--;
                            index++;
                            try {TimeUnit.MILLISECONDS.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}
                } //while End
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
